package wxapp

import (
	"encoding/json"
	"huling/utils/results"
	utils "huling/utils/tool"
	"io/ioutil"

	"github.com/gin-gonic/gin"
)

// 保存分组
func SaveGroup(context *gin.Context) {
	//获取post传过来的data
	body, _ := ioutil.ReadAll(context.Request.Body)
	var parameter map[string]interface{}
	_ = json.Unmarshal(body, &parameter)
	//当前用户
	getuser, _ := context.Get("user")
	user := getuser.(*utils.UserClaims)
	parameter["uid"] = user.ID
	res, err := DB().Table("wxapp_card_group").Data(parameter).Insert()
	if err != nil {
		results.Failed(context, "添加失败", err)
	} else {
		results.Success(context, "添加成功！", res, nil)
	}
}

// 获取分组列表
func GetGroup(context *gin.Context) {
	//当前用户
	getuser, _ := context.Get("user")
	user := getuser.(*utils.UserClaims)
	gList, err := DB().Table("wxapp_card_group").Where("uid", user.ID).Order("id desc").Get()
	if err != nil {
		results.Failed(context, "获取分组列表失败", err)
	} else {
		for _, v := range gList {
			mcount, _ := DB().Table("wxapp_card_folder").Where("group_id", v["id"]).Count()
			v["count"] = mcount
		}
		Count, _ := DB().Table("wxapp_card_folder").Where("add_uid", user.ID).Where("group_id", 0).Count()
		results.Success(context, "获取分组列表", gList, Count)
	}
}
